package com.boluo.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.boluo.business.vo.AuditInfoVO;
import com.boluo.common.constant.BusinessConstants;
import com.boluo.common.core.domain.entity.SysUser;
import com.boluo.common.exception.ServiceException;
import com.boluo.common.utils.ActivitiesUtils;
import com.boluo.common.utils.BigDecimalUtils;
import com.boluo.common.utils.DateUtils;
import com.boluo.system.service.ISysUserService;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boluo.business.mapper.BusServiceItemMapper;
import com.boluo.business.domain.BusServiceItem;
import com.boluo.business.service.IBusServiceItemService;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 服务项Service业务层处理
 *
 * @author boluo
 * @date 2023-04-08
 */
@Service
public class BusServiceItemServiceImpl implements IBusServiceItemService {
    @Resource
    private BusServiceItemMapper busServiceItemMapper;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private RepositoryService repositoryService;

    /**
     * 查询服务项
     *
     * @param id 服务项主键
     * @return 服务项
     */
    @Override
    public BusServiceItem selectBusServiceItemById(Long id) {
        return busServiceItemMapper.selectBusServiceItemById(id);
    }

    /**
     * 查询服务项列表
     *
     * @param busServiceItem 服务项
     * @return 服务项
     */
    @Override
    public List<BusServiceItem> selectBusServiceItemList(BusServiceItem busServiceItem) {
        return busServiceItemMapper.selectBusServiceItemList(busServiceItem);
    }

    /**
     * 新增服务项
     *
     * @param busServiceItem 服务项
     * @return 结果
     */
    @Override
    public int insertBusServiceItem(BusServiceItem busServiceItem) {
        // 参数校验问题
        // 六个参数都要校验
        // 1 自己写代码校验
        // 2 使用 Spring 断言
        // 3 使用 JDK 提供的校验注解(默认注解有限,不能满足所有业务验证需求,自定义注解或者混合使用)
//        String name = busServiceItem.getName();
        /*if (!StringUtils.hasLength(name)) {
            throw new ServiceException("服务项必填");
        }*/
//        Assert.hasLength(name,"服务项必填");

        BigDecimal originalPrice = busServiceItem.getOriginalPrice();
        BigDecimal discountPrice = busServiceItem.getDiscountPrice();
        if (originalPrice.compareTo(discountPrice) < 0) {
            throw new ServiceException("折扣价不能大于原价");
        }

        // 根据是否是套餐,设置其审核状态
        busServiceItem.setAuditStatus(
                busServiceItem.getCarPackage() == BusinessConstants.CAR_PACKAGE_YES
                        ? BusinessConstants.AUDIT_STATUS_INIT
                        : BusinessConstants.AUDIT_STATUS_NOT_NEED);

        // 设置上下架状态
        busServiceItem.setSaleStatus(BusinessConstants.SALE_STATUS_OFF);

        busServiceItem.setCreateTime(DateUtils.getNowDate());
        return busServiceItemMapper.insertBusServiceItem(busServiceItem);
    }

    /**
     * 修改服务项
     *
     * @param busServiceItem 服务项
     * @return 结果
     */
    @Override
    public int updateBusServiceItem(BusServiceItem busServiceItem) {
        // 检验数据
        // 已经采用JDK 的校验注解 加上自己的校验
        // 校验id
        Long id = busServiceItem.getId();
        Assert.notNull(id,"服务项id,不能为空");
        BusServiceItem originBusServiceItem = busServiceItemMapper.selectBusServiceItemById(id);
        Assert.notNull(originBusServiceItem,"你要查询的服务项id 不存在");

        // 校验价格



        BigDecimal originalPrice = busServiceItem.getOriginalPrice();
        BigDecimal discountPrice = busServiceItem.getDiscountPrice();

        // 校验传入的原价和折扣价是否数值并且大于0
        Assert.state(BigDecimalUtils.isBigZero(originalPrice),"原价不能为负数");
        Assert.state(BigDecimalUtils.isBigZero(discountPrice),"折扣价不能为负数");

        if (originalPrice.compareTo(discountPrice) < 0) {
            throw new ServiceException("折扣价不能大于原价");
        }

        // 校验服务项的状态,
        // 表中字段有记录状态的,这种的数据在修改,或删除的时候,都需要判断状态的
        int auditStatus = originBusServiceItem.getAuditStatus();
        Assert.state(auditStatus == BusinessConstants.AUDIT_STATUS_INIT
                || auditStatus == BusinessConstants.AUDIT_STATUS_NOT_NEED,"服务项的审核状态,不可更改");

        int saleStatus = originBusServiceItem.getSaleStatus();
        Assert.state(saleStatus == BusinessConstants.SALE_STATUS_OFF,"服务已经上架,不能改变套餐状态");

        // 根据是否是套餐,设置其审核状态
        busServiceItem.setAuditStatus(
                busServiceItem.getCarPackage() == BusinessConstants.CAR_PACKAGE_YES
                        ? BusinessConstants.AUDIT_STATUS_INIT
                        : BusinessConstants.AUDIT_STATUS_NOT_NEED);
        return busServiceItemMapper.updateBusServiceItem(busServiceItem);
    }

    /**
     * 批量删除服务项
     *
     * @param ids 需要删除的服务项主键
     * @return 结果
     */
    @Override
    public int deleteBusServiceItemByIds(Long[] ids) {
        return busServiceItemMapper.deleteBusServiceItemByIds(ids);
    }

    /**
     * 删除服务项信息
     *
     * @param id 服务项主键
     * @return 结果
     */
    @Override
    public int deleteBusServiceItemById(Long id) {
        return busServiceItemMapper.deleteBusServiceItemById(id);
    }

    @Override
    public int updateBusServiceItemSaleStatus(Long id, Integer saleStatus) {
        // 校验id
        Assert.notNull(id,"服务项id,不能为空");
        BusServiceItem originBusServiceItem = busServiceItemMapper.selectBusServiceItemById(id);
        Assert.notNull(originBusServiceItem,"你要查询的服务项id 不存在");
        // 获取数据库的上下架状态
        Integer saleStatusDB = originBusServiceItem.getSaleStatus();
        // 想要改成的上下架状态
        Assert.notNull(saleStatus,"服务项上下架信息不能为空");

        Assert.state(!saleStatusDB.equals(saleStatus),"服务已是预期状态不需要更改");

        // 校验套餐审核状态
        int auditStatus = originBusServiceItem.getAuditStatus();
        Assert.state(auditStatus == BusinessConstants.AUDIT_STATUS_SUCCESS
                || auditStatus == BusinessConstants.AUDIT_STATUS_NOT_NEED
                ,"服务项当前的审核状态,不支持上下架");



        return busServiceItemMapper.updateBusServiceItemSaleStatus(id,saleStatus);
    }

    @Override
    public AuditInfoVO queryServiceItemAuditInfo(Long id) {
        // 校验

        // 根据服务id查询服务项
        BusServiceItem busServiceItem = busServiceItemMapper.selectBusServiceItemById(id);

        AuditInfoVO auditInfoVO = new AuditInfoVO();
        // 查询店长
        List<SysUser> shopOwners = sysUserService.selectUserByRoleName("店长");
        // 查询财务

        // todo key写死的 拿到折扣价阈值
        // 获取流程对象
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("bus_car_package")
                .singleResult();
        // 获取流程模型
        BpmnModel bpmnModel = repositoryService.getBpmnModel(definition.getId());
        // 服务项价格阈值 (即流程图上定义折扣价判断)
        BigDecimal discountPrice = ActivitiesUtils.getDiscountPrice(bpmnModel, "bus_car_package");
        System.out.println("==========="+discountPrice);

        // 如果价格小于阈值 就不需要财务进行审核
        if (busServiceItem.getDiscountPrice().compareTo(discountPrice) >= 0) {
            List<SysUser> finances = sysUserService.selectUserByRoleName("财务");
            auditInfoVO.setFinances(finances);
        }



        auditInfoVO.setBusServiceItem(busServiceItem);
        auditInfoVO.setShopOwners(shopOwners);
        auditInfoVO.setDiscountPrice(discountPrice);

        /**
         * 获取 流程图上 3000 阈值 前端页面上不用写死
         *
         */

        return auditInfoVO;
    }
}
